import * as vscode from 'vscode';
import { WordCounter } from './WordCounter';

export class StatusBarManager {
    private readonly _item: vscode.StatusBarItem;
    private readonly _counter = new WordCounter();

    constructor() {
        this._item = vscode.window.createStatusBarItem(
            vscode.StatusBarAlignment.Right,
            100
        );
    }
    
    public showWordCounter(editor?: vscode.TextEditor) {
        this.refresh(editor);
        this._item.show();
    }

    public refresh(editor?: vscode.TextEditor) {
        const count = editor ? this._counter.count(editor.document.getText()) : 0;
        this._item.text = `$(book) ${count}`;
    }

    public dispose() {
        this._item.dispose();
    }
}
